// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../../general/Script/component/ComponentBase";
import { AssetsMgr, g_bundleName } from "../../../general/Script/mgr/assetsManager";
import DataMgr from "../../../general/Script/mgr/dataManager";
import GameMgr from "../../../general/Script/mgr/gameManager";
import SoundMgr from "../../../general/Script/mgr/soundManager";
import BlockMoreGMData from "./blockMoreGMData";
import BlockMoreGameLogic from "./blockMoreGameLogic";









const { ccclass, property } = cc._decorator;

@ccclass
export default class BlockMoreGmMgr extends ComponentBase {


    @property(cc.Prefab)
    blockPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    combPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    addScorePrefab: cc.Prefab = null;

    // @property([cc.Prefab])
    blockTouches: cc.Prefab[] = [];

    @property([cc.SpriteFrame])
    blockSp: cc.SpriteFrame[] = [];

    @property(cc.SpriteFrame)
    blockOverSp: cc.SpriteFrame = null;





    @property(cc.Prefab)
    overNode: cc.Prefab = null;

    @property(cc.Prefab)
    settingNode: cc.Prefab = null;

    blockGame: cc.Node = null;
    blockNew: cc.Node = null;
    scoreNode: cc.Node = null;
    // blockEffect:cc.Node = null;
    blockGameEffectRow: cc.Node = null;
    blockGameEffectCol: cc.Node = null;
    scoreEffect: cc.Node = null;
    blockEffect: cc.Node = null;
    blockStar: cc.Node = null;
    blockStep: cc.Node = null;
    scoreLeft: cc.Node = null;

    highScore: cc.Node = null;

    data: BlockMoreGMData = null;
    logic: BlockMoreGameLogic = null;


    gameRow = 10;
    gameCol = 8;
    isGame_over = false;

    pos_touch_start: cc.Vec2 = null;

    touchNode: cc.Node = null;

    sendMoveMsgPos: cc.Vec2 = null;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        super.onLoad();






        this.initGameEvent();

        DataMgr.Ins().setGameData('blockSp', this.blockSp);
        DataMgr.Ins().setGameData('blockOverSp', this.blockOverSp);


        this.data = new BlockMoreGMData(this.node, this);
        this.logic = new BlockMoreGameLogic(this.node, this);




        //初始化游戏地图




        this.setTouchEvent()


    }


    start() {

        


    }


    startGame(reStart: boolean = false) {


    }


    initGameEvent() {
        // GameMgr.Ins().on(`${this.name}_${this.node.uuid}`,'reBlockGame',this.reGameFunc.bind(this))
        GameMgr.Ins().on(`${this.name}_${this.node.uuid}`, 'newBlockGame', this.startGame.bind(this))


    }




    onDestroy(): void {
        super.onDestroy();
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);

        DataMgr.Ins().deleteGameData();
        // DataMgr.Ins().deleteGameData('blockOverSp');

        // DataMgr.Ins().deleteGameData('blockSp');

    }


    newGame(restart = false) {
        console.error('newGame');

        //没有就开始新游戏





        // this.isGame_over = false;
        // this.data.initMap();
        // this.logic.refreshMap(this.data.getMap());

        // this.data.setScore(0);
        // this.logic.updateScore(this.data.getScore());

        // this.logic.gameStart(()=>{
        //     this.newTouchBlock();
        // });

    }



    gameAddScore(data, clear) {
        // if([GameType.star,GameType.step].indexOf(this.gameType) > -1){
        //     return;
        // }
        // console.log('gameAddScore:',data);
        // if(this.gameType === GameType.normal){
        //     this.data.addScore(data);
        //     this.logic.updateScore(this.data.getScore())

        // }else{
        //     const over = this.data.reduceLeftScore(data);
        //     this.logic.updateLeftScore(this.data.getLeftScore());
        //     if(over && !this.isGame_over){
        //         this.gameOver(true);
        //     }
        // }
        // if(clear){
        //     this.data.wantAddScore = 0;
        // }


    }






    gameOver(win: boolean = false) {
        console.log('game over');
        //是否看广告  没看 直接结束

        this.isGame_over = true;

        // DataMgr.Ins().setGameData('score',this.data.getScore());
        DataMgr.Ins().setGameData('win', win);
        this.node.runAction(cc.sequence(cc.delayTime(0.5), cc.callFunc(() => {
            const overNode = cc.instantiate(this.overNode);
            this.node.addChild(overNode);
            overNode.zIndex = 100;
            overNode.scale = 0.1;
            overNode.runAction(cc.scaleTo(0.3, 1).easing(cc.easeIn(2)));
        })))



        // this.logic.gameOver(overTime);
    }

    setTouchEvent() {

        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);

    }

    touchStart(event: cc.Event.EventTouch) {
        if (this.isGame_over) {
            return;
        }
        const pos = event.getLocation();
        var posBlockNew = this.blockNew.convertToNodeSpaceAR(pos);
        this.pos_touch_start = pos


        for (let i in this.blockNew.children) {
            const child = this.blockNew.children[i];
            const rect = child.getBoundingBox();
            if (rect.contains(posBlockNew)) {
                console.log('touch block:', i);
                this.touchNode = child;

                SoundMgr.Ins().playEffect('block_touch', g_bundleName.gameBlock);

            }
        }

    }

    touchMove(event: cc.Event.EventTouch) {

        if (this.isGame_over) {
            return;
        }
        if (this.touchNode == null) {
            return;
        }
        const pos = event.getLocation();
        const offsetY = 100;
        // this.blockNew.setPosition(pos);
        const posNew = cc.v2(pos.x - this.pos_touch_start.x, pos.y - this.pos_touch_start.y);
        var pos_move = event.getDelta()
        this.pos_touch_start.x = this.pos_touch_start.x + pos_move.x
        this.pos_touch_start.y = this.pos_touch_start.y + pos_move.y


    }


    touchEnd(event: cc.Event.EventTouch) {
        if (this.isGame_over) {
            return;
        }
        if (this.touchNode == null) {
            return;
        }




    }

    touchCancel(event: cc.Event.EventTouch) {
        if (this.isGame_over) {
            return;
        }


    }




    reGameCallBack(target, data) {

        this.startGame(true);
    }


    testBtnCallBack() {




    }


    backHomeCallBack(target, data) {
        GameMgr.Ins().emit('overGame');
    }


    settingCallBack(target, data) {
        console.log('setting CallBack')
        const node = cc.instantiate(this.settingNode);
        this.node.addChild(node);
        node.zIndex = 100;
        node.x = 0;
        node.y = 0;
    }

}
